package xyz.labmem.base.sys.module.menu.controller

import cn.dev33.satoken.annotation.SaCheckPermission
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import xyz.labmem.base.core.annotion.BusinessLog
import xyz.labmem.base.core.enums.LogAnnotationOpTypeEnum
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.sys.base.BaseController
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.base.sys.module.menu.entity.SysUserMenus
import xyz.labmem.base.sys.module.menu.entity.response.MenuResponse
import xyz.labmem.base.sys.module.menu.entity.response.MenusResponse
import xyz.labmem.base.sys.module.menu.mapper.SysUserMenusMapper
import xyz.labmem.base.sys.module.menu.service.MenuService

@Api(tags = ["菜单权限"])
@RestController
@RequestMapping("${PathPrefixConst.SYS_PATH_PREFIX}menus")
class SysUserMenusController(
    private var menuService: MenuService,
    private var sysUserMenusMapper: SysUserMenusMapper
)  : BaseController() {

    private val packagePre = "xyz.labmem.main.modular.app."

    @GetMapping("list")
    @ApiOperation(value = "菜单列表")
    fun list(): RV<List<MenusResponse>> {
        val res = ArrayList<MenusResponse>()
        res.add(MenusResponse().apply { id="zhgk";name="综合概述";menusList=menuService.getAmountByModel("${packagePre}zhgk") })
        res.add(MenusResponse().apply { id="gmjj";name="国民经济";menusList=menuService.getAmountByModel("${packagePre}gmjj") })
        res.add(MenusResponse().apply { id="xxly";name="新型领域";menusList=menuService.getAmountByModel("${packagePre}xxly") })
        res.add(MenusResponse().apply { id="jtys";name="交通运输";menusList=menuService.getAmountByModel("${packagePre}jtys") })
        res.add(MenusResponse().apply { id="wqzb";name="武器装备";menusList=menuService.getAmountByModel("${packagePre}wqzb") })
        res.add(MenusResponse().apply { id="zzdy";name="政治动员";menusList=menuService.getAmountByModel("${packagePre}zzdy") })
        res.add(MenusResponse().apply { id="rmfk";name="人民防空";menusList=menuService.getAmountByModel("${packagePre}rmfk") })
        res.add(MenusResponse().apply { id="rmwz";name="人民武装";menusList=menuService.getAmountByModel("${packagePre}rmwz") })
        return success(res)
    }

    @ApiOperation(value = "根据用户编号查询所属菜单")
    @GetMapping(value = ["/{userId}"])
    fun getInfo(@ApiParam(value = "用户id", required = true) @PathVariable userId: String): RV<List<SysUserMenus>>{
        return success(sysUserMenusMapper.selectListAll(userId))
    }

    @SaCheckPermission("super")
    @BusinessLog(title = "批量菜单授权", opType = LogAnnotationOpTypeEnum.ADD)
    @ApiOperation(value = "批量添加用户菜单授权")
    @PostMapping(value = ["/selectAll"])
    fun selectUserAll(@Validated @RequestBody menusResponse: MenuResponse): RV<*> {
        if(getLoginUser().superr==0){
           return failedMsg("只有超级管理员可以操作")
        }
        menusResponse.id?.let { sysUserMenusMapper.deleteUserRole(it) }
        // 新增用户与菜单管理
        val list: MutableList<SysUserMenus> = ArrayList()

        for(menus in menusResponse.menusList!!){
            for(menu in menus.menusList!!){
                if(menu.checked){
                    list.add(SysUserMenus().apply { this.userId = menusResponse.id; this.menuId = menu.prefix })
                }
            }
        }
        return  if(list.size>0) success(sysUserMenusMapper.insertBatchSomeColumn(list))
                else  success()

    }
}